/*
 * Copyright (c) Ulrich Hilger, http://dev.uhilger.de, all rights reserved.
 *
 * Published under the terms and conditions of the Apache License 2.0,
 * see http://www.apache.org/licenses/LICENSE-2.0
 */

package de.uhilger.servicehub.view;

import de.uhilger.servicehub.ServiceHub;
import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import javax.swing.JDialog;
import java.awt.HeadlessException;
import java.awt.event.ActionListener;

import java.awt.Container;
import java.awt.Frame;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.util.ResourceBundle;
import java.util.prefs.Preferences;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

/**
 * <p>A user interface component to display a dialog for user settings.</p>
 *
 * @author Ulrich Hilger, <a href="http://dev.uhilger.de" target="_blank">http://dev.uhilger.de</a>
 * @author Published under the terms and conditions of
 * the <a href="http://www.apache.org/licenses/LICENSE-2.0" target="_blank">Apache License 2.0</a>
 */
public class PrefsDialog extends JDialog implements ActionListener, ItemListener {

  private static final String RN_PREFS_DIALOG_DEFAULT_DB_LOGIN_LABEL = "prefsDialogDefaultDbLoginLabel";
  private static final String RN_PREFS_DIALOG_START_SERVER_ON_LAUNCH_LABEL = "prefsDialogStartServerOnLaunchLabel";
  private static final String RN_PREFS_DIALOG_OK_BUTTON_LABEL = "prefsDialogOkButtonLabel";
  private static final String RN_PREFS_DIALOG_TITLE = "prefsDialogTitle";

	/** the close button */
	private JButton okBtn;

  private JCheckBox defaultDbLogin;
  private JCheckBox startServerOnLaunch;


  /**
	 * Create a new object of class PrefsDialog
	 * @throws HeadlessException
	 */
	public PrefsDialog(Frame owner, ResourceBundle res) throws HeadlessException {
		super(owner);

    Preferences prefs = Preferences.userNodeForPackage(ServiceHub.class);

    setTitle(res.getString(RN_PREFS_DIALOG_TITLE));
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

		defaultDbLogin = new JCheckBox(res.getString(RN_PREFS_DIALOG_DEFAULT_DB_LOGIN_LABEL));
    defaultDbLogin.setSelected(prefs.getBoolean(ServiceHub.PREF_DEFAULT_DB_LOGIN, false));
    defaultDbLogin.addItemListener(this);

		startServerOnLaunch = new JCheckBox(res.getString(RN_PREFS_DIALOG_START_SERVER_ON_LAUNCH_LABEL));
    startServerOnLaunch.setSelected(prefs.getBoolean(ServiceHub.PREF_START_SERVER_ON_LAUNCH, true));
    startServerOnLaunch.addItemListener(this);

    JPanel prefsPanel = new JPanel(new GridLayout(2,1));
    prefsPanel.add(defaultDbLogin);
    prefsPanel.add(startServerOnLaunch);

    okBtn = new JButton(res.getString(RN_PREFS_DIALOG_OK_BUTTON_LABEL));
    okBtn.addActionListener(this);
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(okBtn);

    JPanel centerPanel = new JPanel(new BorderLayout());
    centerPanel.add(prefsPanel, BorderLayout.NORTH);
    centerPanel.setBorder(new EmptyBorder(6, 6, 6, 6));

    setLayout(new BorderLayout());
    Container contentPane = getContentPane();
    contentPane.add(centerPanel, BorderLayout.CENTER);
    contentPane.add(buttonPanel, BorderLayout.SOUTH);
	}

	/**
	 * handle button actions for the close button
	 * @param e ActionEvent
	 */
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if(source.equals(okBtn)) {
			dispose();
		}
	}

  public void itemStateChanged(ItemEvent e) {
    Object src = e.getSource();
    if(src.equals(defaultDbLogin)) {
      Preferences prefs = Preferences.userNodeForPackage(ServiceHub.class);
      prefs.putBoolean(ServiceHub.PREF_DEFAULT_DB_LOGIN, (e.getStateChange() == ItemEvent.SELECTED));
    } else if(src.equals(startServerOnLaunch)) {
      Preferences prefs = Preferences.userNodeForPackage(ServiceHub.class);
      prefs.putBoolean(ServiceHub.PREF_START_SERVER_ON_LAUNCH, (e.getStateChange() == ItemEvent.SELECTED));
    }
  }

}
